package cn.icare.cms.impl;

import cn.icare.cms.ADService;
import cn.icare.cms.ListUtil;
import cn.icare.cms.dao.mapper.ADMapper;
import cn.icare.cms.dao.po.AD;
import cn.icare.cms.model.ro.AddADRO;
import cn.icare.cms.model.ro.EditADRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ADServiceImpl implements ADService {

    @Resource
    private ADMapper adMapper;

    private final Logger logger = LoggerFactory.getLogger(ADServiceImpl.class);


    @Override
    public Integer createOne(AddADRO ro, String actionUserName) {
        try {
            AD ad = new AD();
            ad.setPic(ro.getPic());
            ad.setKind(ro.getKind());
            ad.setUrl(ro.getUrl());
            ad.setSort(ro.getSort());
            ad.setStatus(ro.getStatus());
            ad.setCreator(actionUserName);
            ad.setCreatetime(new Date());
            ad.setMemo(ro.getMemo());
            ad.setType(ro.getType());
            int result = this.adMapper.insertSelective(ad);
            if (result > 0) {
                return ad.getId();
            }
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return 0;
    }

    @Override
    public boolean updateOne(EditADRO ro, String actionUserName) {
        try {
            AD ad = new AD();
            ad.setId(ro.getId());
            ad.setPic(ro.getPic());
            ad.setKind(ro.getKind());
            ad.setUrl(ro.getUrl());
            ad.setSort(ro.getSort());
            ad.setStatus(ro.getStatus());
            ad.setCreator(actionUserName);
            ad.setCreatetime(new Date());
            ad.setMemo(ro.getMemo());
            Integer result = this.adMapper.updateByPrimaryKeySelective(ad);
            return result > 0;
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return false;
    }

    @Override
    public boolean deleteOne(Integer id) {
        Integer result = this.adMapper.deleteByPrimaryKey(id);
        return result > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(AD.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.adMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public AD getOne(Integer id) {
        return this.adMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getAllList(Integer type,Integer status) {
        MsgDatasVO result = new MsgDatasVO();
        try {

            Example example=new Example(AD.class);
            Example.Criteria criteria=example.createCriteria();
            criteria.andEqualTo("type",type);
            if(status!=null && status>-1)
            {
                criteria.andEqualTo("status",status);
            }
            List<AD> dataList = this.adMapper.selectByExample(example);
            if(dataList!=null && dataList.size()>0) {
                dataList = dataList.stream()
                        .sorted(Comparator.comparing(AD::getSort))
                        .collect(Collectors.toList());
            }
            result.setTotal(dataList.size());
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getAllList 异常", e);
        }
        return result;
    }
}
